source('00_util_scripts/mod_seurat.R')

key_cytokine <- c('Ccl20','Tslp','Flt3l','Csf2','Tnf','Il6')

kegg_mva <-
  c('Acat1','Acat2','Hmgcs1','Hmgcs2','Hmgcr','Mvk','Pmvk','Mvd','Idi1','Idi2','Fdps')

mva_fct <- tibble(gene = kegg_mva, ordered = fct_inorder(kegg_mva))

late.kc <- c('Krt1','Krt10','Lor','Ivl','Tgm1','Flg')

# import ---------
mex.path <- list.files('mission/FPP/celik24_Efaecalis', 'gz', full.names = T)

sobj <- mex.path |> read_geo_supp('(uninf|inf).+_(1|2)')

sobj %<>% PercentageFeatureSet('^mt-', col.name = 'mito.ratio')

sobj |> VlnPlot('mito.ratio')

sobj %<>% filter(mito.ratio < 10)

sobj %<>% quick_process_seurat()

mmur <- micerna

mmur <- celldex::MouseRNAseqData()

sobj %<>% mark_cell_type_singler(mmur, new_label = 'mmur.main')

sobj |> DimPlot(group.by = 'mmur.main', split.by = 'group') +
  ggtitle('Mice skin cell 4 dpi of E. faecalis') +
  theme_jpub

publish_pdf('mission/FPP/micefig2/skin.4dpi.umap.split.pdf', width = 100)

sobj |>
  as_tibble() |>
  mutate(.cell, umap_1, umap_2, main.type = mmur.main, group,
         .keep = 'none') |>
  write_csv('mission/FPP/pub_source_data/R3.Q10.figF.4dpi.EF.skin.umap.csv')

sobj |> filter(mmur.main == 'T cells')

sobj |> DotPlot(c('Krt14','Krt15',late.kc), cols = 'RdYlBu')

sobj |> FindAllMarkers(features = c('Krt14','Krt15',late.kc),
                       only.pos = T) |>
  filter(p_val_adj < .05)

wilcox.kc <- c(2,4,8,9,12,14,15,16,21,23,25)

sobj %<>% mutate(mmur.main = ifelse(seurat_clusters %in% wilcox.kc,
                                    'Keratinocytes', mmur.main),
                 group = str_extract(orig.ident, '.+fect'))

sobj |>
  DimPlot()

sobj |> write_rds('mission/FPP/celik24_Efaecalis/celik24.rds')

sobj <- read_rds('mission/FPP/celik24_Efaecalis/celik24.rds')

immune.frac <- sobj |>
  filter(str_detect(mmur.main, 'Macro|T|Gran')) |>
  calc_frac_conf_on_grouped_count(group, mmur.main)

immune.pval <- sobj |>
  filter(str_detect(mmur.main, 'Macro|T|Gran')) |>
  test_on_grouped_count(group, mmur.main)

immune.frac |>
  left_join(immune.pval, join_by(mmur.main == subtype)) |>
  write_csv('4dpi.skin.immune.frac.csv')

# KC -----------
sobj.kc <- sobj |>
  filter(mmur.main == 'Keratinocytes')

sobj.kc %<>% quick_process_seurat(c('orig.ident', 'group'), skip_norm = T)

sobj.kc |>
  filter(group == 'infect') |>
  DotPlot(c('Trpv3', kegg_mva, late.kc), cols = "RdYlBu") +
  RotatedAxis() +
  labs(title = 'V3-hi KC marker in infected KC')

sobj.kc |>
  filter(group != 'infect') |>
  DotPlot(c('Trpv3', kegg_mva, late.kc), cols = "RdYlBu") +
  RotatedAxis() +
  labs(title = 'V3-hi KC marker in uninfected KC')

v3h.mark <- sobj.kc |>
  FindAllMarkers(features = c('Trpv3', kegg_mva, late.kc), only.pos = T) |>
  filter(p_val_adj < .05)

v3h.mark |>
  filter(gene %in% late.kc)

# T cell ---------------
sobj.tc <- sobj |>
  filter(mmur.main == 'T cells')

sobj.tc %<>% quick_process_seurat(skip_norm = T)

t.submarker <- list(Th1 = c('Tbx21','Ifng','Csf2','Tnf','Fasl'),
                    Th2 = c('Gata3','Il4','Il5','Il13'),
                    Th17 = c('Rorc','Il17a','Il17f','Il22'),
                    Treg = c('Foxp3','Il10','Tgfb1'), 'Cd8a',
                    gdT = c('Trgc1','Trgc2','Trdc','Trac','Trbc1','Trbc2'))

sobj.tc |>
  DotPlot(t.submarker, cols = 'RdYlBu') +
  RotatedAxis()
